package com.example.vocab.controller;

import com.alibaba.fastjson.JSON;
import com.example.vocab.entity.MyWord;
import com.example.vocab.entity.User;
import com.example.vocab.entity.Word;
import com.example.vocab.service.WordService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class WordController {

    @Resource(name="wordServiceImpl")
    WordService wordService;

    @RequestMapping("word")
    public List<Word> getWord(@RequestParam(value="cet4", required=true)String cet4,
                              @RequestParam(value="cet6", required=true)String cet6)
    {
        int c4=0;
        int c6=0;
        if(cet4!=null)
            c4=Integer.parseInt(cet4);
        if(cet6!=null)
            c6=Integer.parseInt(cet6);
//        List<Word> words = wordService.getWord();
        int e=0;
        int m=0;
        int h=0;
        if(c4>500&&c6>470)
        {
            e=30;
            m=30;
            h=40;
        }
        else if(c4>600&&c6>530)
        {
            e=10;
            m=40;
            h=50;
        }
        else if(c4>650&&c6>600)
        {
            e=0;
            m=30;
            h=70;
        }
        else
        {
            e=40;
            m=40;
            h=20;
        }
        List<Word> words=new ArrayList<>();
        List<Word> easyWord = wordService.getEasyWord(e);
        List<Word> midWord = wordService.getMidWord(m);
        List<Word> hardWord = wordService.getHardWord(h);
        for (Word word : easyWord) {
            words.add(word);
        }
        for (Word word : midWord) {
            words.add(word);
        }
        for (Word word : hardWord) {
            words.add(word);
        }
        return words;
    }

    @RequestMapping("myword")
    public String addWord(@RequestBody HashMap<String,String[]> map,
                          @RequestParam(value="uid", required=true) String uid)
    {
        String[] wids=map.get("unknow");
        int error=0;
        for (String id : wids) {
            if(id!=null&&uid!=null)
            {
                Integer wid=Integer.parseInt(id);
                if(wordService.getWordByWid(wid)!=null)
                    continue;
                else{
                    Integer res = wordService.addMyWord(Integer.parseInt(uid), Integer.parseInt(id));
                    if(res!=1)
                    {
                        System.out.println("出错了");
                        error++;
                    }
                }
            }
        }
        HashMap<String,String> resMap=new HashMap<>();
        resMap.put("flag","success");
        resMap.put("msg","出错了"+error+"个！");
        String data=JSON.toJSONString(resMap);
        return data;
    }

    @RequestMapping("getmyword")
    public List<Word> getMyWord(@RequestParam(value="uid", required=true) String uid)
    {
        List<MyWord> myWords=null;
        if(uid!=null)
            myWords = wordService.getMyWord(Integer.parseInt(uid));
        List<Word> words=new ArrayList<>();
        for (MyWord word : myWords) {
            Word wr = wordService.getUnKnow(word.getWid());
            words.add(wr);
        }
        return words;
    }

    @RequestMapping("delete")
    public String delMyWord(@RequestBody HashMap<String,String[]> map,
                            @RequestParam(value="uid", required=true) String uid)
    {
        int id=Integer.parseInt(uid);
        int error=0;
        HashMap<String,String> resMap=new HashMap<>();
        String[] wids=map.get("wid");
        for (String wid : wids) {
            if(wid!=null&&uid!=null)
            {
                int i = wordService.delMyWord(id, Integer.parseInt(wid));
                if(i!=1){
//                    System.out.println("出错了");
                    error++;
                }
            }
        }
        resMap.put("flag","success");
        resMap.put("msg","出错了"+error+"个！");
        return JSON.toJSONString(resMap);
    }
}
